package com.mo2323.merchant.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.mo2323.common.VO;
import com.mo2323.common.VOList;
import com.mo2323.common.util.AjaxObject;
import com.mo2323.service.merchant.IFieldService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/field")
public class FieldController extends BaseController {
    @Reference
    private IFieldService iFieldService;

    @RequestMapping(value = "/getFieldLayoutInfo", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public AjaxObject getFieldLayoutInfo(@RequestParam int vid, @RequestParam String date, String startTime, String useTime) {
        try {
            Map<String, Object> fieldLayoutInfo = iFieldService.getFieldLayoutInfo(vid, date, startTime, useTime);
            return AjaxObject.ok().data(fieldLayoutInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxObject.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/getLayoutInfo", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public AjaxObject getLayoutInfo(@RequestParam int vid) {
        try {
            Map<String, Object> layoutInfoForIOS = iFieldService.getLayoutInfo(vid);
            return AjaxObject.ok().data(layoutInfoForIOS);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxObject.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/list", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public AjaxObject getFieldList(@RequestParam int vid,@RequestParam String date) {
        try {
            Map<String, Object> fieldList= iFieldService.getFieldList(vid,date);
            return AjaxObject.ok().data(fieldList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxObject.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/LightList", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public AjaxObject getFieldLightList(@RequestParam int vid) throws Exception {
        try {

            List<VO> fieldLightList = iFieldService.getFieldLightList(vid);
            return AjaxObject.ok().put("data", VOList.I(fieldLightList).getData(true));
        } catch (Exception e) {
            logger.error("getFieldLightList Exception：", e);
            return AjaxObject.error("getFieldLightList Exception：" + e.getMessage());
        }
    }

    @RequestMapping(value = "/openLightByField", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public AjaxObject openLightByField(@RequestParam String vid,@RequestParam String fid,@RequestParam String light) throws Exception {
        try {
            boolean flag = iFieldService.openLightByField(vid,fid,light);
            return AjaxObject.ok().put("data", flag == true ? "开灯成功" : "开灯失败");
        } catch (Exception e) {
            logger.error("openLightByField Exception：", e);
            return AjaxObject.error("openLightByField Exception：" + e.getMessage());
        }
    }

    @RequestMapping(value = "/closeLightByField", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public AjaxObject closeLightByField(@RequestParam String vid,@RequestParam String fid,@RequestParam String light) throws Exception {
        try {
            boolean flag = iFieldService.closeLightByField(vid,fid,light);
            return AjaxObject.ok().put("data", flag == true ? "关灯成功" : "关灯失败");
        } catch (Exception e) {
            logger.error("closeLightByField Exception：", e);
            return AjaxObject.error("closeLightByField Exception：" + e.getMessage());
        }
    }

}
